var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
//引入swagger
var swaggerJSDoc = require('swagger-jsdoc');

var studentRouter = require('./routes/sutdent');

var app = express();
// 定义swagger
var swaggerDefinition = {
  info: {
    title: '种猪系统api',
    version: '2.0.0',
    description: '学生系统api测试',
    contact: {
      name:"李小黑",  //联系人
      email:"Lee_black@163.com" , //联系人邮件
      url:"http://Lee_black.com/support" //公司的网址
    }
  },
  host: 'localhost:3000',
  basePath: '/',
};

// swagger的文档的配置
var options = {
  // 引入swagger的定义
  swaggerDefinition: swaggerDefinition,
  //扫描api生成api的文档
  //** 表示所有的包  *.js 表示所有的js文件
  apis: ['./routes/**/*.js'],
};

//初始化swagger-jsdoc
var swaggerSpec = swaggerJSDoc(options);

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({
  extended: false
}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));


app.use('/student', studentRouter);

// 读取swagger的信息
app.get('/swagger.json', function(req, res) {
  res.setHeader('Content-Type', 'application/json');
  res.send(swaggerSpec);
});
// catch 404 and forward to error handler
app.use(function (req, res, next) {
  next(createError(404));
});

// error handler
app.use(function (err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;